﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;


namespace Qetza.TeamFoundation.WorkItemQuery.Admin
{
    /// <summary>
    /// Contains utility methods.
    /// </summary>
    public static class Utility
    {
        #region Methods

        /// <summary>
        /// Gets the tool name.
        /// </summary>
        /// <value>
        /// The tool name.
        /// </value>
        public static string ToolName
        {
            get
            {
                return System.IO.Path.GetFileNameWithoutExtension(
                    Environment.GetCommandLineArgs()[0]
                    );
            }
        }

        /// <summary>
        /// Writes the specified text to the console breaking lines at the end of words.
        /// </summary>
        /// <param name="text">
        /// The text to write.
        /// </param>
        public static void WriteWrappedLines(string text)
        {
            Console.WriteLine(); //separate the output from the command line to make it more readable
            foreach (var line in text.Break(Console.BufferWidth - 1))
            {
                Console.WriteLine(line);
            }
        }

        /// <summary>
        /// Asks for a confirmation.
        /// </summary>
        /// <param name="text">
        /// The confirmation text.
        /// </param>
        /// <returns>
        /// <b>true</b> if the user confirms; otherwise <b>false</b>.
        /// </returns>
        public static bool Confirm(string text)
        {
            // display confirmation message
            var yes = ResourceStrings.YesAnswer;
            var no = ResourceStrings.NoAnswer;
            var msg = string.Format(ResourceStrings.ConfirmationMessage, text, yes, no);

            Utility.WriteWrappedLines(msg);

            // read answer
            while (true)
            {
                var resp = Console.ReadLine().Trim();
                if (yes == resp)
                {
                    return true;
                }

                if (no == resp)
                {
                    return false;
                }

                Utility.WriteWrappedLines(string.Format(ResourceStrings.InvalidAnswer, msg));
            }
        }

        #endregion
    }
}
